<?php
/* return 0 if not exits */
function TEST_EXIST_ITEM($msql)
{
	if(DBSTYLE == 1){
		$rs=mysql_query($msql);
		//echo "$msql<br>";
		if($rs) {
			if(mysql_num_rows($rs)>0) {
				return 1;
			}else {
				return 0;
			}
		}
		return 0;
	}else{
		$rs=mssql_query($msql);
		//echo $msql;
		if($rs) {
			if(mssql_num_rows($rs)>0) {
				return 1;
			}else {
				return 0;
			}
		}
		return 0;	
	}
}

//thay the gia tri nhu submit file, button...
function loadtemplate($arr)
{
	$html = "";
	$html = loadHtml($arr["layout_html"],"");
	if (count($arr) > 0){
		foreach ($arr as $key=>$value){
			//$$key = $value;	//gan gia tri vao bien
			$pattern = "@$key@";
			$replacement = "$value";
			$html = ereg_replace($pattern, $replacement, $html);
		}//end for
	}
	return $html;	
}//loadtemplate


//Kiem tra du lieu truoc khi delete
function CheckItemsRelationship($arr_id, $arr_table)
{
	$result = "";
	$id_list = array();
	
	if(is_array($arr_id) && is_array($arr_table)){
	
		//print_r ($arr_id);echo"<br>";
		//print_r ($arr_table);echo"<br>";
		
		for($i=0;$i<count($arr_id);$i++){
			foreach($arr_table as $table => $arr_field){
				//echo "table=> $table<br>";
				$id = $arr_id[$i];
				//echo "id=$id<br>";
				foreach($arr_field as $field){
					$sql = "select * from $table where $field = $id";
					//echo "id_list = "; print_r ($id_list);echo "<br>";
					//echo "$sql<br>";
					
					if (TEST_EXIST_ITEM($sql)){
						if(count($id_list)>0){
							$tontai = 0;
					
							//echo "hien tai : table=$table, value=$id<br>";
							//echo "----------<br>";
							foreach($id_list as $table_tmp=>$array)
							{
								//print_r ($array);echo "<br>";
								foreach($array as $key=>$value)
								{
									$giatri = $id_list[$table_tmp][$key];
									//echo "id_list[$table_tmp][$key] = $giatri<br>";
									if($table == $table_tmp && $value == $id){
										$tontai = 1;
										//echo "ton tai <br>";
									}
								}
								//echo "<br>";
							}
							//echo "------------<br>";
														
							if(!$tontai){
								$id_list[$table][] = $id;
								
							}
							
						}else{
							$id_list[$table][] = $id;
						}
					}
				}
				//echo "<br>";
			}
			//echo "--------------------------------------<br>";
		}
		
		//print_r ($id_list);echo "<br>";
		
		if(count($id_list) > 0){
			$result = "B&#7841;n kh&ocirc;ng th&#7875; x&oacute;a, b&#7903;i v&igrave; ch&uacute;ng &#273;&atilde; &#273;&#432;&#7907;c s&#7917; d&#7909;ng trong b&#7843;ng d&#7919; li&#7879;u sau:<br>";

			foreach($id_list as $table=>$array)
			{
				$result .= "- $table: ";
				//print_r ($array);echo "<br>";
				foreach($array as $key=>$value)
				{
					//echo "$key<br>";
					$result .= "$value ";
				}
				$result .= "<br>";
			}
		}

	}else{
		$result = "Var input not is array";
	}	
	return $result;
}//end CheckItemsRelationship

function check_parent_in_childs_list($arr_childs_list, $parent_id)
{
	$result = 0;
	foreach ($arr_childs_list as $arr_cat){
		if ( $parent_id == $arr_cat[id]){
			$result = 1;
			break;
		}
	}
	return $result;
}


function get_cat_tree_sanpham($parent_id = '0', $lang_id, $spacing = '', $exclude = '', $category_tree_array = '', $include_itself = false) {
	global $dbconn;
	
	//echo "parent_id: $parent_id<br>";
	//echo "duan_id: $duan_id<br>";
	//echo "exclude: $exclude<br>";
	
	if (!is_array($category_tree_array)) $category_tree_array = array();
	if ( (sizeof($category_tree_array) < 1) && ($exclude != '0') ) $category_tree_array[] = array('id' => '0', 'text' => "-----");
	
	if ($include_itself) {
		$category_query = tep_db_query("select cat_name as name from sanpham_cat where catid = '" . (int)$parent_id . "'");
		$category = tep_db_fetch_array($category_query);
		$category_tree_array[] = array('id' => $parent_id, 'text' => $category['name']);
	}
	
	$sql = "select catid as id, cat_name as name, cat_parent as parent_id from sanpham_cat ";
	$sql.= "where cat_parent = '" . (int)$parent_id . "' and langid = '".(int)$lang_id."' order by cat_order, cat_name";
	//echo "$sql<br>";
	$rs= new db_query($dbconn,$sql);
	while ($rs->getrow())
	{
		if ($exclude != $rs->row['id']) {
			$category_tree_array[] = array(
									'id' => $rs->row['id'], 
									'text' => $spacing . $rs->row['name']
									);
		}
		$category_tree_array = get_cat_tree_sanpham($rs->row['id'], $lang_id, $spacing . '&nbsp;&nbsp;&nbsp;', $exclude, $category_tree_array);
	}
	return $category_tree_array;
}


function get_cat_tree_noidung($parent_id = '0', $lang_id, $spacing = '', $exclude = '', $category_tree_array = '', $include_itself = false) {
	global $dbconn;
	
	//echo "parent_id: $parent_id<br>";
	//echo "duan_id: $duan_id<br>";
	//echo "exclude: $exclude<br>";
	
	if (!is_array($category_tree_array)) $category_tree_array = array();
	if ( (sizeof($category_tree_array) < 1) && ($exclude != '0') ) $category_tree_array[] = array('id' => '0', 'text' => "-----");
	
	if ($include_itself) {
		$category_query = tep_db_query("select contents_name as name from contents where contents_id = '" . (int)$parent_id . "'");
		$category = tep_db_fetch_array($category_query);
		$category_tree_array[] = array('id' => $parent_id, 'text' => $category['name']);
	}
	
	$sql = "select contents_id as id, contents_name as name, contents_parent as parent_id from contents ";
	$sql.= "where contents_parent = '" . (int)$parent_id . "' and langid = '".(int)$lang_id."' order by contents_order, contents_name";
	//echo "$sql<br>";
	$rs= new db_query($dbconn,$sql);
	while ($rs->getrow())
	{
		if ($exclude != $rs->row['id']) {
			$category_tree_array[] = array(
									'id' => $rs->row['id'], 
									'text' => $spacing . $rs->row['name']
									);
		}
		$category_tree_array = get_cat_tree_noidung($rs->row['id'], $lang_id, $spacing . '&nbsp;&nbsp;&nbsp;', $exclude, $category_tree_array);
	}
	return $category_tree_array;
}
function get_arr_menu_fullpath($reverse_id = '0', $category_tree_array = '') {
	global $dbconn, $langid;
	
	if (!is_array($category_tree_array)) $category_tree_array = array();
	
	$sql = "select contents_id as id, contents_name as name, contents_parent as parent_id from contents ";
	$sql.= "where contents_id = '" . (int)$reverse_id . "' and langid = '".(int)$langid."' order by contents_order, contents_name";
	//echo "$sql<br>";
	$rs= new db_query($dbconn,$sql);
	while ($rs->getrow())
	{
		$parent_id = $rs->row['parent_id'];
		$parent_name = get_values("select contents_name from contents where contents_id = '".$parent_id."'");
		$category_tree_array[] = array(
								'id' => $parent_id, 
								'text' => $parent_name,
								'link' => "cat.php?catid=".$parent_id."&id=".$parent_id."&langid=".$langid
								);

		$category_tree_array = get_arr_menu_fullpath($rs->row['parent_id'], $category_tree_array);
	}
	return $category_tree_array;
}

function get_string_menu_fullpath ($root_id)
{
	global $subcatid, $catid;
	//echo "subcatid:$subcatid - catid: $catid<br>";
	if ($subcatid > 0) {
		$id_reverse = $subcatid;
	}else{
		$id_reverse = $catid;
	}
	
	$curent_name = get_values("select contents_name from contents where contents_id = '".$id_reverse."'");
	
	//get_cat_tree_noidung('0',"$session_ngonngu_admin", '', '','',false);
	$arr_full_path = get_arr_menu_fullpath($id_reverse);
	//print_r ($arr_full_path);
	$arr_full_path_reverse = array_reverse($arr_full_path);
	//print_r ($arr_full_path_reverse);
	
	$flag = false;
	$html = '<img src="../images_layout/icon_path_new.gif" width="19" height="15" align="absmiddle">';
	$tmp = '<a href="@link@" class="menu_path">@name@</a>';
	
	$i=1;
	$level=0;
	$total = count($arr_full_path_reverse);
	$muiten = "&nbsp;&gt;&nbsp;";
	foreach ($arr_full_path_reverse as $arr){

		if ($flag){
			$html .= $tmp;
			$html = ereg_replace("@link@", $arr['link'], $html);
			$html = ereg_replace("@name@", $arr['text'], $html);
			$level++;
			if ($i < $total){				
				$html .= $muiten;				
			}
			
		}


		if ($arr['id'] == $root_id){
			$flag = true;
		}
		$i++;
		
	}
	//echo "<br>i: $i - total: $total - level: $level<br>";
	if ($level > 0){
		$html .= $muiten.'<span class="menu_path_nolink">'.$curent_name.'</span>';
	}else{
		$html .= '<span class="menu_path_nolink">'.$curent_name.'</span>';
	}
	
	return $html;
}

// Output a form pull down menu
function draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
	//echo "name: $name<br>";
	//echo "values: $values<br>";
	//echo "default: $default<br>";
	
	$field = '<select name="' . tep_output_string($name) . '"';
	
	if (tep_not_null($parameters)) $field .= ' ' . $parameters;
	
	$field .= '>';
	
	if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
	
	for ($i=0, $n=sizeof($values); $i<$n; $i++) {
		$field .= '<option value="' . tep_output_string($values[$i]['id']) . '"';
		if ($default == $values[$i]['id']) {
			$field .= ' SELECTED';
		}
		
		$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
	}
	$field .= '</select>';
	
	if ($required == true) $field .= TEXT_FIELD_REQUIRED;
	
	return $field;
}//end function



function get_array_sql($sql)
{
	global $dbconn;
	$arr = array();
	$rs = new db_query($dbconn, $sql);
	$i=1;
	while ($rs->getrow())	 {
		$arr[$rs->row[0]] = $rs->row[1]."";
	}
	//print_r ($arr);
	return $arr;
}



function get_array_fulldown($sql, $require = true)
{
	global $dbconn;
	$arr = array();
	//echo "require: $require<br>";
	if (!$require){
		$arr[] = array(
				'id' => "0",
				'text' => "------ Select ------"
				);
	}	
	$rs = new db_query($dbconn, $sql);
	$i=1;
	while ($rs->getrow())	 {
		$arr[] = array(
			'id' => $rs->row[0], 
			'text' => $rs->row[1].""
			);		
	}
	//print_r ($arr);
	return $arr;
}

function get_array_fulldown_by_array($array, $require = true)
{
	$arr = array();
	//echo "require: $require<br>";
	if (!$require){
		$arr[] = array(
				'id' => "0",
				'text' => "------ Chon ------"
				);
	}
	foreach ($array as $value=>$text)
	{
		$arr[] = array(
			'id' => $value, 
			'text' => $text.""
			);		
	}
	//print_r ($arr);
	return $arr;
}



function mktime_dmy($date)
{
	$arr_date = split("[/.-]",$date);
	//print_r ($arr_date);
	if (count($arr_date) > 0){

		return mktime(0,0,0,$arr_date[1], $arr_date[0], $arr_date[2]);
	}else{
		return 0;
	}
}
function date_dmy($timestamp)
{
	if ($timestamp > 0){
		return date("d-m-Y", $timestamp);	
	}else{
		return "&nbsp;";
	}
}
function today_timestamp()
{
	return mktime(0, 0, 0, date("m"), date("d"), date("Y"));
}
function today_fulltimestamp()
{
	return mktime(date("H"),date("i"), 0, date("m"), date("d"), date("Y"));
}
function get_file_date()
{
	$NgayThang = mktime (date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));
	$file_date = date("YmdHis",$NgayThang);
	return $file_date;
}

function safe_sql($text)
{
	$text = str_replace(array("\\",";","'","="), "", $text);
	return $text;
}

function get_name_product($proid){
	global $langid;
	if ($langid > 0){
		$sql = "select ten from sanpham where id = '$proid'";
	}else{
		$sql = "select ten_vn from sanpham where id = '$proid'";
	}
	//echo "$sql<br>";
	$name = get_values($sql);
	return $name;
}//end get_name_product


function nut_over($nut, $nut_over, $name, $link="#", $target="")
{
	$path = "../images_layout/";
	$html = "<a href=\"".$link."\" $target onMouseOut=\"Nut_swapImgRestore()\" onMouseOver=\"Nut_swapImage('".$name."','','".$path.$nut_over."',1)\"><img src='".$path.$nut."' name='".$name."' border='0' ></a>";
	return $html;
}


function Gan_Editor_Vao_Field($arr)
{
	$html = "";
	$tmp = "document.forms.frm_editor.elements.@name@.value = oEdit_@name@.getHTMLBody();\n";
	if (is_array($arr)){
		if (count($arr) > 0){
			foreach($arr as $name){
				$html .= $tmp;
				$html = ereg_replace("@name@", $name, $html);
			}
		}
	}
	
	return $html;
}
function get_string_array($arr)
{
	$result = "";
	if (is_array($arr)){
		$total = count($arr);
		if($total > 0){
			for($i=0;$i<$total;$i++){
				if($i == $total -1){
					$result .= $arr[$i];
				}else{
					$result .= $arr[$i].", ";
				}
			}//end for			
		}//end if		
	}//end if
	
	return $result;
}

function get_head_menu()
{
	global $catid, $subcatid;
	
	if ($subcatid > 0){
		$id = $subcatid;
	}else{
		$id = $catid;
	}
	
	$sql = "select contents_motangan from contents where contents_id = '$id'";
	//echo "$sql<br>";
	$head = get_values($sql);
	return $head;
	
}

function getNumber(&$name, $value=0) {
	if(!isset($name))
		$name = $value;

	if(!is_numeric($name))
		return  $value;
	else 
		return $name;		
}
	
function getString(&$name, $value = "") {
	if(!isset($name))
		$name = $value;
	return $name;
}

function get_user_id()
{
	global $HTTP_SESSION_VARS;
	
	//print_r($HTTP_SESSION_VARS);echo "<br>";
	
	$user_id = 	$HTTP_SESSION_VARS['session_user_login']['user_id'];
	$user_name = $HTTP_SESSION_VARS['session_user_login']['user_name'];
	
	$sql = "select * from thanhvien where id='$user_id' and username = '$user_name'";
	$check = TEST_EXIST_ITEM($sql);
	//echo "check: $check<br>";
	if($check){
		return $user_id;
	}else{
		return "";
	}
}

function update_taikhoan($userid)
{
	global $dbconn;
	
	$sql = "select * from thanhvien where id = '$userid'";
	$rs = new db_query($dbconn, $sql);
	if ($rs->getrow()){
		$tien_1 = $rs->row['tien_1'];
		$tien_2 = $rs->row['tien_2'];
	}
	if ($tien_1 >= DONVI_dv){
		$tien_1 = $tien_1 - DONVI_dv;
		$sql = "update thanhvien set tien_1 = '$tien_1' where id = '$userid'";
		$dbconn->execute($sql);
	}else{
		if($tien_2 >= DONVI_dv){
			$tien_2 = $tien_2 - DONVI_dv;
			$sql = "update thanhvien set tien_2 = '$tien_2' where id = '$userid'";
			$dbconn->execute($sql);
		}
	}
	//cap nhat ngay dang tin
	$ngaydangtin = today_timestamp();
	$sql = "update thanhvien set ngaydangtin='$ngaydangtin' where id = '$userid'";
	//echo "$sql<br>";
	$dbconn->execute($sql);
}

function kiemtra_sodu($userid)
{
	global $dbconn;
	$sql = "select * from thanhvien where id = '$userid'";
	$rs = new db_query ($dbconn, $sql);
	if ($rs->getrow()){
		$tien_1 = $rs->row["tien_1"];
		$tien_2 = $rs->row["tien_2"];
	}
	$tien = $tien_1 + $tien_2;
	return $tien;
}//end kiemtra_sodu


	function get_tooltip($text, $width = SP_IMAGES_CHITIET_WIDTH)
	{		
		$text = str_replace(array("\\",";","'","\n","\r","\t"), "", $text);
		//echo "text 1: $text<br>";
		$tooltip = " onmouseover=\"ddrivetipMK('$text', ".$width.")\" onmouseout=\"hideddrivetipMK()\" ";
		return $tooltip;
	}//get_tooltip
	
	function split_para($str, $max_word)
	{
		$html = "";
	
		if($str != ""){
			$arr = split(" ",$str,$max_word);
			//print_r ($arr);
			$total = count($arr);
					
			$i = 0;
			for ($i=0;$i<count($arr);$i++){
				if ($i < $max_word - 1){
					$html .= $arr[$i]." ";
				}
			}
			
			if($total > $max_word){
				$html .= "...";
			}
		}	
		return $html;
	}//end split_para

	function loadtmpfile($tmp_file)
	{
		$arr = array();
		$tmp = loadHtml($tmp_file, "");
		$arr['layout'] = get_sub_string_2($tmp,"<!--layout_0-->","<!--layout_1-->");
		$arr['begin'] = get_sub_string_2($tmp,"<!--list_begin_0-->","<!--list_begin_1-->");
		$arr['body'] = get_sub_string_2($tmp,"<!--list_body_0-->","<!--list_body_1-->");
		$arr['blank'] = get_sub_string_2($tmp,"<!--list_blank_0-->","<!--list_blank_1-->");
		$arr['folder'] = get_sub_string_2($tmp,"<!--list_folder_0-->","<!--list_folder_1-->");
		$arr['top'] = get_sub_string_2($tmp,"<!--list_top_0-->","<!--list_top_1-->");
		$arr['end'] = get_sub_string_2($tmp,"<!--list_end_0-->","<!--list_end_1-->");
		return $arr;	
	}


	function get_sub_string_2($string, $st1, $st2)
	{
		$start_position = strpos($string, "$st1") + strlen($st1);
		$end_position = strpos($string, "$st2");
		$result = substr($string, $start_position, $end_position - $start_position);
		return $result;
	}

	function format_gia($value, $donvi)
	{
		if($donvi < 2){//VND
			$gia     = number_format($value,0,",",".") . " VND";
		}else{//USD
			$gia = "$".number_format($value,2,",",".");
		}
		return $gia;
	}


?>